package com.j3l11234.myy_sign.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.j3l11234.myy_sign.entity.AttendEntity;
import com.j3l11234.myy_sign.entity.UserEntity;
import com.j3l11234.myy_sign.model.AttendModel;
import com.j3l11234.myy_sign.model.SettingModel;
import com.j3l11234.myy_sign.util.MyException;
import com.j3l11234.myy_sign.util.PrivilegeCheck;


@Controller
@RequestMapping("/home")
public class HomeController {
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private SettingModel settingModel;
	@Autowired
	private AttendModel attendModel;
	
	@RequestMapping("/index")
	@PrivilegeCheck(privilege = UserEntity.Worker, needLogin = true)
    public Object index(ModelMap model,HttpSession session) {
		
		model.addAttribute("navbar_item", "home");
		model.addAttribute("sidebar_item", "index");
		UserEntity userEntity = (UserEntity) session.getAttribute("user");
		
		//生成近7天的日期
		Date startDate = null, endDate = null;
		Calendar calendar = Calendar.getInstance();
		endDate = calendar.getTime();
		calendar.add(Calendar.DATE, -7);
		startDate = calendar.getTime();
		
		//查询签到签退记录
		try {
			List<AttendEntity> attendList = attendModel.getUserAttend(startDate, endDate, userEntity.getId());
			model.addAttribute("attendList", attendList);
			model.addAttribute("inTime", settingModel.getSigninTime());	
			model.addAttribute("outTime", settingModel.getSignoutTime());	
		} catch (MyException e) {
			e.printStackTrace();
		}
		
		//读取公告板数据
		try {
			model.addAttribute("board_text", settingModel.getBoardText());
		} catch (MyException e) {
			e.printStackTrace();
		}
		
        return "home/index";
    }
	
	@RequestMapping("/signin")
	@PrivilegeCheck(privilege = UserEntity.Worker, needLogin = true)
    public Object signin(
    		@RequestParam(value="start_date", required = false) String strStartDate,
    		@RequestParam(value="end_date", required = false) String strEndDate,
    		ModelMap model, HttpSession session) {
		
		model.addAttribute("navbar_item", "home");
		model.addAttribute("sidebar_item", "signin");
		UserEntity userEntity = (UserEntity) session.getAttribute("user");
		
		//解析开始时间和结束时间
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null, endDate = null;
        try {
        	startDate = dateFormat.parse(strStartDate);
		} catch (Exception e) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -7);
			startDate = calendar.getTime();
		}
        try {
        	endDate = dateFormat.parse(strEndDate);
		} catch (Exception e) {
			endDate = new Date();
		}
        model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		
        //执行查询
		try {
			List<AttendEntity> attendList = attendModel.getUserAttend(startDate, endDate, userEntity.getId());
			model.addAttribute("attendList", attendList);
			model.addAttribute("inTime", settingModel.getSigninTime());	
		} catch (MyException e) {
			e.printStackTrace();
		}
		
		return "home/signin";
    }
	
	@RequestMapping("/signout")
	@PrivilegeCheck(privilege = UserEntity.Worker, needLogin = true)
    public Object signout(
    		@RequestParam(value="start_date", required = false) String strStartDate,
    		@RequestParam(value="end_date", required = false) String strEndDate,
    		ModelMap model, HttpSession session) {
		
		model.addAttribute("navbar_item", "home");
		model.addAttribute("sidebar_item", "signout");
		UserEntity userEntity = (UserEntity) session.getAttribute("user");
		
		//解析开始时间和结束时间
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null, endDate = null;
        try {
        	startDate = dateFormat.parse(strStartDate);
		} catch (Exception e) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -7);
			startDate = calendar.getTime();
		}
        try {
        	endDate = dateFormat.parse(strEndDate);
		} catch (Exception e) {
			endDate = new Date();
		}
        model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		
        //执行查询
		try {
			List<AttendEntity> attendList = attendModel.getUserAttend(startDate, endDate, userEntity.getId());
			model.addAttribute("attendList", attendList);
			model.addAttribute("outTime", settingModel.getSignoutTime());
		} catch (MyException e) {
			e.printStackTrace();
		}
		
        return "home/signout";
    }
}
